package com.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class t4_方法 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> clazz = Class.forName("com.reflection.Student");
        //获取所有公共方法
        Method[] m1 = clazz.getMethods();
        for (Method m : m1) {
            System.out.println(m);
        }
        //获取所有方法(包括私有)
        Method[] m2 = clazz.getDeclaredMethods();
        for (Method m : m2) {
            System.out.println(m);
        }
        //获取指定方法
        Method m = clazz.getMethod("show");//空参
        Method m3 = clazz.getMethod("show",String.class);
        //获取指定方法(包括私有)
        Method m4 = clazz.getDeclaredMethod("eat", String.class);


        //反射调用方法
        Student s = new Student();  //需要对象调用方法
        Method e = clazz.getDeclaredMethod("eat", String.class);
        //私有方法(需要暴力)
        e.setAccessible(true);
        e.invoke(s, "金哥");

    }
}
